package com.swolin.exception;

import com.swolin.controller.ExceptionController;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice(assignableTypes = {ExceptionController.class})
@ResponseBody
public class GlobalExceptionHandler {
    ErrorResponse illegalArgumentResponse = new ErrorResponse(new IllegalArgumentException("参数错误！"));
    ErrorResponse resourseNotFoundResponse = new ErrorResponse(new ResourceNotFoundException("sorry, the resource not found"));

    public ResponseEntity<ErrorResponse> exceptionHandler(Exception e){
        if(e instanceof IllegalArgumentException){
            return ResponseEntity.status(400).body(illegalArgumentResponse);
        }else if(e instanceof ResourceNotFoundException){
            return ResponseEntity.status(404).body(resourseNotFoundResponse);
        }
        return null;
    }
}
